package com.dkd.sbtz.service.impl;

import java.util.List;
import com.dkd.common.utils.DateUtils;
import com.dkd.sbtz.domain.Sbtz;
import com.dkd.sbtz.domain.vo.spare.SpareVO;
import com.dkd.sbtz.mapper.SbtzMapper;
import com.dkd.sbtz.mapper.SpareMapper;
import com.dkd.sbtz.service.ISpareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.sbtz.mapper.AssetAssessmentMapper;
import com.dkd.sbtz.domain.AssetAssessment;
import com.dkd.sbtz.service.IAssetAssessmentService;

/**
 * 设备评估Service业务层处理
 * 
 * @author xzj
 * @date 2025-10-23
 */
@Service
public class AssetAssessmentServiceImpl implements IAssetAssessmentService 
{
    @Autowired
    private AssetAssessmentMapper assetAssessmentMapper;

    @Autowired
    private ISpareService spareService;

    @Autowired
    private SbtzMapper sbtzMapper;
    /**
     * 查询设备评估
     * 
     * @param id 设备评估主键
     * @return 设备评估
     */
    @Override
    public AssetAssessment selectAssetAssessmentById(Long id)
    {
        return assetAssessmentMapper.selectAssetAssessmentById(id);
    }

    /**
     * 查询设备评估列表
     * 
     * @param assetAssessment 设备评估
     * @return 设备评估
     */
    @Override
    public List<AssetAssessment> selectAssetAssessmentList(AssetAssessment assetAssessment)
    {
        return assetAssessmentMapper.selectAssetAssessmentList(assetAssessment);
    }

    /**
     * 新增设备评估
     * 
     * @param assetAssessment 设备评估
     * @return 结果
     */
    @Override
    public int insertAssetAssessment(AssetAssessment assetAssessment)
    {
        assetAssessment.setCreateTime(DateUtils.getNowDate());

        Long assetId = assetAssessment.getAssetId();
        if (assetId != null) {
            // 1. 查询是否有未删除记录
            AssetAssessment existing = assetAssessmentMapper.selectByAssetId(assetId);

            // 2. 如果存在且未删除，则先删除
            if (existing != null && existing.getIsDelete() == 0) {
                assetAssessmentMapper.logicDeleteByAssetId(existing.getAssetId());
            }
        }
        if (assetAssessment.getIsEnterPart() == 1) {
            SpareVO spareVO = assembleSpareVO(assetAssessment.getAssetId(), assetAssessment);
            spareService.insertSpareWithVariants(spareVO);
        }
        return assetAssessmentMapper.insertAssetAssessment(assetAssessment);
    }

    private SpareVO assembleSpareVO(Long assetId, AssetAssessment assetAssessment) {
        Sbtz assetNode = sbtzMapper.getById(assetId);

        SpareVO spareVO = new SpareVO();
        spareVO.setName(assetNode.getName());
        spareVO.setCode(null);
        spareVO.setStockTotal(null);
        spareVO.setStockAvailable(null);
        spareVO.setIsSpecial(assetAssessment.getIsSpecial());
        spareVO.setStatus(0);
        spareVO.setRemark("评估页面同步");
        spareVO.setOrderDate(0);
        spareVO.setSpareClass(assetNode.getNodeType());
        spareVO.setMinStock(assetAssessment.getSpareCount());
        spareVO.setMaxStock(assetAssessment.getSpareCount()+30);
        spareVO.setSpecification(assetNode.getSpecification());
        spareVO.setModel(assetNode.getModel());
        spareVO.setMaterial(assetNode.getMaterial());
        spareVO.setBrand(assetNode.getBrand());
        spareVO.setManufacturer(assetNode.getManufacturer());
        spareVO.setDesignTemp(assetNode.getDesignTemp());
        spareVO.setDesignPress(assetNode.getDesignPress());
        spareVO.setMotorPower(assetNode.getMotorPower());
        spareVO.setAssetId(assetId);
        return spareVO;
    }

    /**
     * 修改设备评估
     * 
     * @param assetAssessment 设备评估
     * @return 结果
     */
    @Override
    public int updateAssetAssessment(AssetAssessment assetAssessment)
    {
        assetAssessment.setUpdateTime(DateUtils.getNowDate());
        return assetAssessmentMapper.updateAssetAssessment(assetAssessment);
    }

    /**
     * 批量删除设备评估
     * 
     * @param ids 需要删除的设备评估主键
     * @return 结果
     */
    @Override
    public int deleteAssetAssessmentByIds(Long[] ids)
    {
        return assetAssessmentMapper.deleteAssetAssessmentByIds(ids);
    }

    /**
     * 删除设备评估信息
     * 
     * @param id 设备评估主键
     * @return 结果
     */
    @Override
    public int deleteAssetAssessmentById(Long id)
    {
        return assetAssessmentMapper.deleteAssetAssessmentById(id);
    }
}
